from django.db import models
import datetime
from StartGroup.models import Group
from django.forms import ModelForm
from django import forms


class Poll(models.Model):
    group_web_address = models.ForeignKey(Group, to_field='group_web_address')
    poll_topic = models.CharField(max_length=200)
    status = models.IntegerField()
    pub_date = models.DateTimeField('Created on', auto_now_add=True, db_index=True)
    def __unicode__(self):
        return self.poll_topic
    def was_published_today(self):
        return self.pub_date.date() == datetime.datetime.today()
        was_published_today.short_description = 'Published today?'
        
class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()
    def __unicode__(self):
        return self.choice

def PollForm(exclude_list, *args, **kwargs):
    class MyPollForm(ModelForm):
        class Meta:
            model = Poll
            exclude = exclude_list
        def __init__(self):
            super(MyPollForm, self).__init__(*args, **kwargs)
             
    return MyPollForm()

def ChoiceForm(exclude_list, *args, **kwargs):
    class MyChoiceForm(ModelForm):
        class Meta:
            model = Choice
            exclude = exclude_list
            
        def __init__(self):
            super(MyChoiceForm, self).__init__(*args, **kwargs)
            
        def choice_validation(self, bound):
            if bound:
                return True
            else:
                return False
    return MyChoiceForm()
